package pl.smath.gui;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
/** 
 *	Testowy program zamieniający wyrażenie arytmetyczne na klasyczną postać książkową.
 *
 * @author Piotr Mech
 *
*/
public class GuiDemo extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Initialize all swing objects.
    private JFrame f = new JFrame("Basic GUI"); //create Frame
    private JPanel pnlNorth = new JPanel(); // North quadrant 
    private JPanel pnlCenter = new JPanel(); // Center quadrant

	// Buttons some there is something to put in the panels
    private JButton btnDrawTree = new JButton("Narysuj AST");

    // Menu
    private JMenuBar mb = new JMenuBar(); // Menubar
    private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item
    private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
    private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry
    
    private JTextField txfExpression = new JTextField("(1+2)*(3+4)", 40);
    private TreeGraph treeGraph = new TreeGraph();
    
    /** Constructor for the GUI */
    public GuiDemo(){
		// Set menubar
        f.setJMenuBar(mb);
        
        btnDrawTree.addActionListener(new ListenBtnDrawTree());
        
		//Build Menus
        mnuFile.add(mnuItemQuit);  // Create Quit line
        mnuHelp.add(mnuItemAbout); // Create About line
        mb.add(mnuFile);        // Add Menu items to form
        mb.add(mnuHelp);

        pnlNorth.add(txfExpression);
        //pnlNorth.add(btnDrawExpression);
        pnlNorth.add(btnDrawTree);
        
        pnlCenter.setLayout(new BorderLayout());
        pnlCenter.add(treeGraph, BorderLayout.CENTER);
                
        f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
        f.getContentPane().add(pnlCenter, BorderLayout.CENTER);
        
        
		// Allows the Swing App to be closed
        f.addWindowListener(new ListenCloseWdw());
		
		//Add Menu listener
        mnuItemQuit.addActionListener(new ListenMenuQuit());
    }
    
    public class ListenBtnDrawExpression implements ActionListener{
        public void actionPerformed(ActionEvent e){
            
        	String expression = txfExpression.getText();
        }
    }
    
    public class ListenBtnDrawTree implements ActionListener{
        public void actionPerformed(ActionEvent e){
            
        	String expression = txfExpression.getText();
        	
        	treeGraph.draw(expression);
        }
    }
    
    public class ListenBtnSimplifyExpression implements ActionListener{
        public void actionPerformed(ActionEvent e){
            
        	String expression = txfExpression.getText();
        }
    }
	
    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);         
        }
    }
	
    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);         
        }
    }
	
    public void launchFrame(){
        
    	f.setSize(1100, 600);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //f.pack(); 
        f.setVisible(true);
    }
    
    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g); 

    }

    
    public static void main(String args[]){
        GuiDemo gui = new GuiDemo();
        gui.launchFrame();
    }
}
